layui.use(['table', 'jquery'], function () {
    const table = layui.table,
        $ = layui.jquery;

    showTime('#startDate');
    showTime('#endDate');

    /**
     * 获取当前登录用户
     * @type {*}
     */
    let result = loginUserAjax();

    findAll();
    findCodeByType('dailyStatus', $("#status"));

    function findAll() {
        let headMaster = result.data.username;
        let username = $('#username').val();
        let title = $('#title').val();
        let status = $('#status').val();
        let startDate = $('#startDate').val();
        let endDate = $('#endDate').val();
        let params = {
            headMaster: headMaster,
            username: username,
            title: title,
            status: status
        };

        if (startDate !== undefined && startDate != null && startDate !== '') {
            params['startDate'] = startDate
        }
        if (endDate !== undefined && endDate != null && endDate !== '') {
            params['endDate'] = endDate
        }

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'userCode', width: '10%', title: '用户编号'}
            , {field: 'username', width: '10%', title: '用户名'}
            , {field: 'title', width: '20%', title: '标题'}
            , {field: 'dailyTime', width: '15%', title: '日期', sort: true}
            , {field: 'commitTime', width: '18%', title: '提交时间', sort: true}
            , {
                field: 'status', width: 120, title: '状态', align: 'center', fixed: 'right', templet: e => {
                    return getDailyStatus(e);
                }
            }
            , {fixed: 'right', width: 160, align: 'center', toolbar: '#operating'}
        ]];

        table.render({
            elem: '#dataList'
            , url: LOCAL_CROSS_URL + "/daily/findAll"
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 15, 30]
            , limit: 10
            , title: '日报表'
            , cols: cols
        });
    }

    //监听行工具事件
    table.on('tool(dataList)', function (obj) {
        const data = obj.data
            , layEvent = obj.event;
        if (layEvent === 'show') {
            showDaily(data.id);
        }
        if (layEvent === 'exportWord') {
            exportWord(data);
        }
    });


    /**
     * 查看
     * @param id
     */
    function showDaily(id) {
        sessionStorage.setItem("dailyId", id);
        commonLayerOpenArea('日报详情', ["100%", "500px"], 'dailyShow.html')
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll()
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#headMaster').val('');
        $('#username').val('');
        $('#title').val('');
        $('#status').val('');
        $('#startDate').val('');
        $('#endDate').val('');
    });

    /**
     * 导出word
     * @param data
     */
    function exportWord(data) {
        let result = commonAjax('/daily/exportWord', JSON.stringify(data), 'post');
        if (result === null) {
            layerMsgOperatingSuccess();
        } else {
            layerMsgOperatingFail();
        }
    }


});